<template>
  <div>
    <el-row style="text-align: left">
      <el-col :span="24">
        <el-button size="medium"
                   type="success"
                   @click="myInsert()">新增
        </el-button>
      </el-col>
    </el-row>
    <div style="margin-top: 20px;">
      <el-table v-loading="listLoading"
                :data="result.jobTypes"
                border
                :header-cell-style="headClass"
                :cell-style="rowClass"
                style="width: 100%">
        <el-table-column
            prop="jobTypeId"
            label="工作类型ID"
            width="180">
        </el-table-column>
        <el-table-column
            prop="jobTypeName"
            label="工作类型名称"
            width="180">
        </el-table-column>
        <el-table-column
            label="操作"
            width="300">
          <template slot-scope='scope'>
            <el-button type="primary" size="mini" @click="myUpdate(scope.row.jobTypeId)">修改</el-button>
            <el-button type="danger" size="mini" @click="myDel(scope.row.jobTypeId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {deleteJobType, getJobTypes, postCreateJobType, putUpdateJobType} from "@/api";

export default {
  name: "jobType",
  data() {
    return {
      listLoading: false,
      result: {
        jobTypes: []
      },
      param: {},

    }
  },
  mounted() {
  },
  created() {
    this.myList()
  },
  methods: {
    //组件方法
    headClass() {
      return "text-align:center"
    },
    rowClass() {
      return "text-align:center"
    },
    //方法
    myInsert() {
      this.$prompt('请输入工作类型名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        postCreateJobType({jobTypeName: value}).then(() => {
          this.$message.success('添加成功')
          this.myList()
        })
      }).catch(() => {
        this.$message.info('取消输入');
      });
    },
    myUpdate(item) {
      this.$prompt('请输入工作类型名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        putUpdateJobType({jobTypeId: item,jobTypeName: value}).then(() => {
          this.$message.success('添加成功')
          this.myList()
        })
      }).catch(() => {
        this.$message.info('取消输入');
      });
    },
    myDel(item) {
      this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteJobType(item).then(()=>{
          this.$message.success('删除成功')
          this.myList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    myList() {
      this.listLoading = true
      getJobTypes().then(res => {
        this.result.jobTypes = res.data.jobTypes
        this.listLoading = false
      })
    }
  },
}
</script>

<style scoped>

</style>